{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic usage of beautifulsoup\n",
    "**First we still need to open a [page](https://morvanzhou.github.io/static/scraping/basic-structure.html), then we can apply beautifulsoup on this page's html.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html>\n<html lang=\"cn\">\n<head>\n\t<meta charset=\"UTF-8\">\n\t<title>Scraping tutorial 1 | 莫烦Python</title>\n\t<link rel=\"icon\" href=\"https://morvanzhou.github.io/static/img/description/tab_icon.png\">\n</head>\n<body>\n\t<h1>爬虫测试1</h1>\n\t<p>\n\t\t这是一个在 <a href=\"https://morvanzhou.github.io/\">莫烦Python</a>\n\t\t<a href=\"https://morvanzhou.github.io/tutorials/scraping\">爬虫教程</a> 中的简单测试.\n\t</p>\n\n</body>\n</html>\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "from urllib.request import urlopen\n",
    "\n",
    "# if has Chinese, apply decode()\n",
    "html = urlopen(\"https://morvanzhou.github.io/static/scraping/basic-structure.html\").read().decode('utf-8')\n",
    "print(html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Parsing this html using a method called lxml, create a soup object. You can simply \"h1\" or \"p\" to call the heading 1 and paragraph tag from soup.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<h1>爬虫测试1</h1>\n\n <p>\n\t\t这是一个在 <a href=\"https://morvanzhou.github.io/\">莫烦Python</a>\n<a href=\"https://morvanzhou.github.io/tutorials/scraping\">爬虫教程</a> 中的简单测试.\n\t</p>\n"
     ]
    }
   ],
   "source": [
    "soup = BeautifulSoup(html, features='lxml')\n",
    "print(soup.h1)\n",
    "print('\\n', soup.p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Or using some helpful functions to find tags. Access the attribute of found tags using a key just like what you do in a python dictionary.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n ['https://morvanzhou.github.io/', 'https://morvanzhou.github.io/tutorials/scraping']\n"
     ]
    }
   ],
   "source": [
    "all_href = soup.find_all('a')\n",
    "all_href = [l['href'] for l in all_href]\n",
    "print('\\n', all_href)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
